package com.google.code.swingonfire.component.clock;

import java.util.Calendar;

import javax.swing.event.EventListenerList;

import com.google.code.swingonfire.utility.Utils;

public class AbstractJClockModel implements JClockModel
{

    private final EventListenerList listeners = new EventListenerList();

    private Calendar selected;

    public AbstractJClockModel()
    {
        super();

        selected = Calendar.getInstance();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void addClockModelListener(JClockModelListener listener)
    {
        listeners.add(JClockModelListener.class, listener);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void removeClockModelListener(JClockModelListener listener)
    {
        listeners.remove(JClockModelListener.class, listener);
    }

    protected void fireClockModelSelectionChanged(Calendar oldValue, Calendar newValue, boolean correctRotation)
    {
        for (JClockModelListener listener : listeners.getListeners(JClockModelListener.class))
        {
            listener.clockModelSelectionChanged(oldValue, newValue, correctRotation);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Calendar getSelected()
    {
        return selected;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setSelected(Calendar selected, boolean correctRotation)
    {
        System.out.println("trying...");
//        if (!Utils.equals(this.selected, selected))
//        {
            System.out.println("success");
            Calendar oldValue = this.selected;

            this.selected = selected;

            fireClockModelSelectionChanged(oldValue, selected, correctRotation);
//        }
    }

}
